package cn.iocoder.yudao.module.erp.service.contractpaymentplan.contractpaymentplandetail;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.contractpaymentplandetail.ContractPaymentPlanDetailPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.contractpaymentplandetail.ContractPaymentPlanDetailSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.contractpaymentplan.contractpaymentplandetail.ContractPaymentPlanDetailDO;
import cn.iocoder.yudao.module.erp.dal.mysql.contractpaymentplan.contractpaymentplandetail.ContractPaymentPlanDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.CONTRACT_PAYMENT_PLAN_DETAIL_NOT_EXISTS;

/**
 * 合同付款计划明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ContractPaymentPlanDetailServiceImpl implements ContractPaymentPlanDetailService {

    @Resource
    private ContractPaymentPlanDetailMapper contractPaymentPlanDetailMapper;

    @Override
    public Long createContractPaymentPlanDetail(ContractPaymentPlanDetailSaveReqVO createReqVO) {
        // 插入
        ContractPaymentPlanDetailDO contractPaymentPlanDetail = BeanUtils.toBean(createReqVO, ContractPaymentPlanDetailDO.class);
        contractPaymentPlanDetailMapper.insert(contractPaymentPlanDetail);
        // 返回
        return contractPaymentPlanDetail.getId();
    }

    @Override
    public void updateContractPaymentPlanDetail(ContractPaymentPlanDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateContractPaymentPlanDetailExists(updateReqVO.getId());
        // 更新
        ContractPaymentPlanDetailDO updateObj = BeanUtils.toBean(updateReqVO, ContractPaymentPlanDetailDO.class);
        contractPaymentPlanDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteContractPaymentPlanDetail(Long id) {
        // 校验存在
        validateContractPaymentPlanDetailExists(id);
        // 删除
        contractPaymentPlanDetailMapper.deleteById(id);
    }

    @Override
        public void deleteContractPaymentPlanDetailListByIds(List<Long> ids) {
        // 校验存在
        validateContractPaymentPlanDetailExists(ids);
        // 删除
        contractPaymentPlanDetailMapper.deleteByIds(ids);
        }

    private void validateContractPaymentPlanDetailExists(List<Long> ids) {
        List<ContractPaymentPlanDetailDO> list = contractPaymentPlanDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONTRACT_PAYMENT_PLAN_DETAIL_NOT_EXISTS);
        }
    }

    private void validateContractPaymentPlanDetailExists(Long id) {
        if (contractPaymentPlanDetailMapper.selectById(id) == null) {
            throw exception(CONTRACT_PAYMENT_PLAN_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public ContractPaymentPlanDetailDO getContractPaymentPlanDetail(Long id) {
        return contractPaymentPlanDetailMapper.selectById(id);
    }

    @Override
    public PageResult<ContractPaymentPlanDetailDO> getContractPaymentPlanDetailPage(ContractPaymentPlanDetailPageReqVO pageReqVO) {
        return contractPaymentPlanDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByPaymentPlanId(Long paymentPlanId) {
        contractPaymentPlanDetailMapper.deleteByPaymentPlanId(paymentPlanId);
    }

    @Override
    public void deleteByPaymentPlanIds(List<Long> paymentPlanIds) {
        contractPaymentPlanDetailMapper.deleteByPaymentPlanIds(paymentPlanIds);
    }

    @Override
    public List<ContractPaymentPlanDetailDO> getByPaymentPlanId(Long paymentPlanId) {
        return contractPaymentPlanDetailMapper.selectByPaymentPlanId(paymentPlanId);
    }

}